<template>
	<view class="">
		<cu-custom bgColor="bg-black" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">回复</block>
		</cu-custom>
		<view class="cu-form-group">
			<textarea maxlength="-1" @input="textareaAInput" placeholder="请输入内容"></textarea>
		</view>
		<view class="cu-form-group" style="padding-top: 20px;">
			<view class="grid col-3 grid-square flex-sub">
				<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
					<image :src="imgList[index]" mode="aspectFill"></image>
					<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
						<text class='cuIcon-close'></text>
					</view>
				</view>
				<view class="solids" @tap="ChooseImage" v-if="imgList.length<9">
					<text class='cuIcon-cameraadd'></text>
				</view>
			</view>
		</view>
		<view class="cu-bar bg-white">
			<view class="action">

			</view>
			<view class="action">
				{{imgList.length}}/9
			</view>
		</view>
		<view class="sub_btn_box">
			<view class="sub_pic">
			</view>
			<view class="btn-group ">
				<button class="cu-btn bg-red round shadow-blur" style="margin-left:20upx;" @click="handSub">立即回复</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				checked: true,
				textareaAValue: '',
				imgList: [],
				rowItem: {},
				isflg: '',
				ifLouzhu: ''
			};
		},
		onLoad(e) {
			this.rowItem = JSON.parse(e.row)
			if (e.isflg) {
				this.isflg = e.isflg
			}
			if (e.ifLouzhu) {
				this.ifLouzhu = e.ifLouzhu
			}
			let userID = JSON.parse(uni.getStorageSync('USER'))
			console.log(this.rowItem, 'item', userID, this.rowItem.userId)
		},
		methods: {
			handSub() {
				if (this.textareaAValue == '') {
					this.print('请输入回帖内容')
				} else {
					uni.showLoading({
						title: '加载中...',
						icon: 'none'
					})
					var timestamp = (new Date()).getTime()
					let userID = JSON.parse(uni.getStorageSync('USER')).id
					let pId = ''
					console.log(this.rowItem, JSON.parse(uni.getStorageSync('USER')))
					if (this.isflg == 1) {
						pId = this.rowItem.id
					} else {
						pId = this.rowItem.yuUserNoticList.length != 0 ? this.rowItem.yuUserNoticList[this.rowItem.yuUserNoticList.length -
							1].id : this.rowItem.id
					}
					let newForm = {
						userId: userID,
						orderId: this.rowItem.orderId,
						isFloor: this.ifLouzhu,
						hotNum: 0,
						createTime: timestamp,
						content: this.textareaAValue,
						parentId: pId
					}
					let pages = getCurrentPages(); // 当前页面
					let beforePage = pages[pages.length - 2];
					this.http.addDetails(newForm).then(res => {
						uni.hideLoading()
						console.log(res, '回帖')
						this.print(res.message)
						setTimeout(() => {
							uni.navigateBack({
								success: function() {
									beforePage.onLoad(); // 执行前一个页面的onLoad方法
								}
							})
						}, 1000)


					})
				}

			},
			ChooseCheckbox(e) {
				let items = this.checkbox;
				let values = e.currentTarget.dataset.value;

				for (let i = 0, lenI = items.length; i < lenI; ++i) {

					if (items[i].value == values) {
						items[i].checked = !items[i].checked;
						// break
					} else {
						items[i].checked = false
						console.log(items[i].checked, '===', values)
					}
				}
			},
			textareaAInput(e) {
				this.textareaAValue = e.detail.value
			},
			ChooseImage() {
				uni.chooseImage({
					count: 9, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						if (this.imgList.length != 0) {
							this.imgList = this.imgList.concat(res.tempFilePaths)
						} else {
							this.imgList = res.tempFilePaths
						}
					}
				});
			},
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				uni.showModal({
					title: '删除',
					content: '确定要删除吗？',
					cancelText: '再看看',
					confirmText: '确定',
					success: res => {
						if (res.confirm) {
							this.imgList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.sub_btn_box {
		width: 100%;
		height: 154upx;
		padding: 0 30upx;
		background: rgba(255, 255, 255, 1);
		display: flex;
		align-items: center;
		justify-content: space-between;
		position: fixed;
		bottom: 0;
		box-sizing: border-box;

		.btns {
			display: flex;

		}

		.sub_pic {
			.com_pic {
				color: #323233;
				font-size: 32upx;
				font-weight: bold;
			}

			.vip_pic {
				color: #E02020;
				font-size: 30upx;
				margin-top: 12upx;
			}
		}
	}
</style>
